package sw6.visualschedule;

import android.app.Dialog;
import android.content.Context;
import android.os.CountDownTimer;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

/**
 * Shows a count down dialog. Is able to both count down in hh:mm:ss format, and count down seconds.   
 */
public class Countdown extends Dialog {

    private final Context mContext;
    
	public static final int SECONDSCOUNT = 0;
	public static final int CLOCKCOUNTDOWN = 1;
	
	private ImageView mDigit1;
	private ImageView mDigit2;
	private ImageView mDigit3;
	private ImageView mDigit4;
	private ImageView mDigit5;
	private ImageView mDigit6;
	private ImageView mDigit7;
	private ImageView mDigit8;
	
	/**
	 * Constructs a Countdown Dialog.
	 * 
	 * @param context	an activity context that contains R.layout.conutdown
	 * @param countdowntype	must be Dialog.SECONDSCOUNT (seconds format) or Dialog.CLOCKCOUNTDOWN (hh:mm:ss format) 
	 * @param countMs	the amount of milliseconds to count down
	 */
	public Countdown(final Context context, final int countdowntype, final long countMs) {
		super(context);
		
		mContext = context;
		
		setContentView(R.layout.countdown);
		setViews();
		
		if (countdowntype == CLOCKCOUNTDOWN) {
			normalTimer(countMs);
		} else {
			secondsOnly(countMs);
		}
	}
	
	/**
	 * Constructs a Countdown Dialog.
	 * 
	 * @param context	an activity context that contains R.layout.conutdown
	 * @param theme	see super
	 * @param countdowntype	must be Dialog.SECONDSCOUNT (seconds format) or Dialog.CLOCKCOUNTDOWN (hh:mm:ss format) 
	 * @param countMs	the amount of milliseconds to count down
	 */
	public Countdown(final Context context, final int theme, final int countdowntype, final long countMs) {
		super(context, theme);
		
		mContext = context;
		
		setContentView(R.layout.countdown);
		setViews();
		
		if (countdowntype == CLOCKCOUNTDOWN) {
			normalTimer(countMs);
		} else {
			secondsOnly(countMs);
		}
	}

	/**
	 * Constructs a Countdown Dialog.
	 * 
	 * @param context	an activity context that contains R.layout.conutdown
	 * @param cancelable	see super
	 * @param cancelListener	see super
	 * @param countdowntype	see super
	 * @param countMs	the amount of milliseconds to count down
	 */
	public Countdown(final Context context, final boolean cancelable, final OnCancelListener cancelListener, final int countdowntype, final long countMs) {
		super(context, cancelable, cancelListener);
		
		mContext = context;
		
		setContentView(R.layout.countdown);
		setViews();
		
		if (countdowntype == CLOCKCOUNTDOWN) {
			normalTimer(countMs);
		} else {
			secondsOnly(countMs);
		}
	}
	
	private void setViews() {
	    mDigit1 = (ImageView) findViewById(R.id.digital1);
        mDigit2 = (ImageView) findViewById(R.id.digital2);
        mDigit3 = (ImageView) findViewById(R.id.digital3);
        mDigit4 = (ImageView) findViewById(R.id.digital4);
        mDigit5 = (ImageView) findViewById(R.id.digital5);
        mDigit6 = (ImageView) findViewById(R.id.digital6);
        mDigit7 = (ImageView) findViewById(R.id.digital7);
        mDigit8 = (ImageView) findViewById(R.id.digital8);
	}
	
	private void secondsOnly(final long countMs) {
	    
		setTitle(mContext.getString(R.string.countdown_time_until));
		
		new CountDownTimer(countMs, 1000) {

		     public void onTick(long timer) {
		         timer = timer / 1000; //We want seconds		         		         
		         
		         int pos = 0;
		         for (char number : String.valueOf(timer).toCharArray()) {
		        	 switch (pos) {
					case 0:
						setNumber( Character.getNumericValue(number), mDigit1);
						break;
					case 1:
						setNumber(Character.getNumericValue(number), mDigit2);
						break;
					case 2:
						setNumber(Character.getNumericValue(number), mDigit3);
						break;
					case 3:
						setNumber(Character.getNumericValue(number), mDigit4);
						break;
					case 4:
						setNumber(Character.getNumericValue(number), mDigit5);
						break;
					case 5:
						setNumber(Character.getNumericValue(number), mDigit6);
						break;
					case 6:
						setNumber(Character.getNumericValue(number), mDigit7);
						break;						
					case 7:
						setNumber(Character.getNumericValue(number), mDigit8);
						break;							
					default:
						break;
					}
		        	 pos++;
		         }
		         
		         switch (String.valueOf(timer).length()) {
				case 1:
					mDigit2.setVisibility(View.GONE);
					break;
		        case 2:
					mDigit3.setVisibility(View.GONE);
					break;
				case 3:
					mDigit4.setVisibility(View.GONE);
					break;	
				case 4:
					mDigit5.setVisibility(View.GONE);
					break;	
				case 5:
					mDigit6.setVisibility(View.GONE);
					break;	
				case 6:
					mDigit7.setVisibility(View.GONE);
					break;	
				case 7:
					mDigit8.setVisibility(View.GONE);
					break;					

				default:
					break;
				}
		     }

		     public void onFinish() {
		         dismiss();
		     }
		  }.start();
	}	
	
	private void normalTimer(final long countMs) {
		mDigit3.setImageResource(R.drawable.digit_colon);
		mDigit6.setImageResource(R.drawable.digit_colon);
		
		setTitle(mContext.getString(R.string.countdown_time_until));
		
		new CountDownTimer(countMs, 1000) {

		     public void onTick(long timer) {
		         timer = timer / 1000; //We want seconds
		         
		         final int hours = (int) (timer / (60 * 60) );
		         timer = timer - (hours * 60 * 60);		         		         
		         
		         final int minutes = (int) (timer / 60);
		         timer = timer - (minutes * 60);
		         
		         final int seconds = (int) (timer);
		         
		         int hour1 = 0;
		         int hour2 = 0;
		         
		         int minute1 = 0;
		         int minute2 = 0;
		         
		         int seconds1 = 0;
		         int seconds2 = 0;
		         		         
		         if (hours >= 10) {
		        	 hour1 = Integer.valueOf( String.valueOf(hours).substring(0,1) );
		        	 hour2 = Integer.valueOf( String.valueOf(hours).substring(1) );
		         } else {
		        	 hour1 = 0;
		        	 hour2 = hours;
		         }
		         
		         if (minutes >= 10) {
		        	 minute1 = Integer.valueOf( String.valueOf(minutes).substring(0,1) );
		        	 minute2 = Integer.valueOf( String.valueOf(minutes).substring(1) );
		         } else {
		        	 minute1 = 0;
			         minute2 = minutes;
		         }
		         
		         if (seconds >= 10) {
		        	 seconds1 = Integer.valueOf( String.valueOf(seconds).substring(0,1) );
		        	 seconds2 = Integer.valueOf( String.valueOf(seconds).substring(1) );
		         } else {
		        	 seconds1 = 0;
			         seconds2 = seconds;
		         }		         
		         
		         setNumber(hour1, mDigit1);
		         setNumber(hour2, mDigit2);
		         setNumber(minute1, mDigit4);
		         setNumber(minute2, mDigit5);
		         setNumber(seconds1, mDigit7);
		         setNumber(seconds2, mDigit8);
		     }

		     public void onFinish() {
		         dismiss();
		     }
		  }.start();
	}
	
	private void setNumber(final int number, final ImageView view) {
		switch (number) {
		case 0:	
			view.setImageResource(R.drawable.digit0);
			break;
		case 1:	
			view.setImageResource(R.drawable.digit1);
			break;
		case 2:	
			view.setImageResource(R.drawable.digit2);
			break;
		case 3:	
			view.setImageResource(R.drawable.digit3);
			break;
		case 4:	
			view.setImageResource(R.drawable.digit4);
			break;
		case 5:	
			view.setImageResource(R.drawable.digit5);
			break;
		case 6:	
			view.setImageResource(R.drawable.digit6);
			break;
		case 7:	
			view.setImageResource(R.drawable.digit7);
			break;
		case 8:	
			view.setImageResource(R.drawable.digit8);
			break;
		case 9:	
			view.setImageResource(R.drawable.digit9);
			break;			
		case -1:	
			view.setImageResource(R.drawable.digit_colon);
			break;			
		default:
			view.setImageResource(R.drawable.digit_colon);
			break;
		}
	}
		
	/**
	 * Handles onTouchEvents. Dialog will always be dismissed on touch.
	 */
	@Override
	public boolean onTouchEvent(final MotionEvent event) {				
		this.dismiss();
		return true;
	}		
}
